<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require '../Class/autoload.php';
	//require_once('Class/UpYun/vendor/autoload.php');
	use Upyun\Upyun;
	use Upyun\Config;
	session_start();


	//测试专用区
	if($_REQUEST['type'] == 'test'){
		require_once('../Class/UpYun/vendor/autoload.php');
		$serviceConfig = new \Upyun\Config($config['upyun_bucketname'],$config['upyun_username'],$config['upyun_password']);
		$up = new \Upyun\Upyun($serviceConfig);
		$res = $up->usage('data_cache');
		dump($res);

		exit('aaaaaa');
	}


	//===== 遍历所有县库并删除商品的过期的图片(数据库及upyun)
	if($_REQUEST['type'] == 'clear_img'){
		$ourl = 'http://shop-cache-upyun.bm123.top/CityInfo/CityOpen.html';
		$res = CurlForGet($ourl);
		if(preg_match('/^Gets\(.*\)$/',$res)){
			preg_match_all('/\[.*\]/', $res, $res2);
			if($res2[0][0]){
				$res3 = json_decode($res2[0][0],1);
				foreach($res3 as $v){
					$ip = IpDecrypt($v['db_host']);
					$pdo = MyPdo::init($ip,$v['db_name'],$config['county_db_user'],$config['share_db_pwd'],true);
					$sql = 'select id,addr from shop_img where gid=0 and fid=0 and expire < ?';
					$res4 = $pdo->dql($sql,array(time()- 10));
					if($res4){
						foreach($res4 as $v){
							$res5 = UpyunDelFile($config,$v['addr']);
							if($res5){
								$sql2 = 'delete from shop_img where id = ?';
								$res6 = $pdo->dml($sql2,array($v['id']));
								dump($res6);
							}
						}
					}
				}
			}
		}
		exit;
	}








	//===== 生成验证码
	if($_REQUEST['type'] == 'code'){
		require_once "../Class/Plugin/VCode.class.php";
		if(CheckOrigin()){
			Code\VCode::GetCode();
		}
		exit;
	}







	//===== ### 请求token

	if($_REQUEST['type'] == 'token'){
		echo $token = md5(time()).md5(rand());
		$_SESSION[$_REQUEST['token_name']]= $token;
		exit;
	}





	//===== ### 验证token

	$token_arr = array(		// 跳过token验证的type项 添加到这里

	);
	if(!in_array($_REQUEST['type'],$token_arr)){
		if(!@$_REQUEST['remote_token']){	//验证本地token
			if(!@$_REQUEST['token'] || $_REQUEST['token'] != $_SESSION[$_REQUEST['type']]){
				$_REQUEST['token'] = md5(rand());
				exit('10001');
			}
			$_REQUEST['token'] = md5(rand());
		}else{		//验证远程token
			if(!CheckRemoteToken($_REQUEST['remote_token'])){
				exit('10002');	//远程token 验证失败
			}
		}
	}


	switch ($_REQUEST['type']) {

		//关闭或封禁砍价商品
		case 'FreeGoodsClose':
			$free_id = intval(I('free_id','post'));
			$c_type = I('c_type','post');	//有值为封禁，无值为关闭
			if(!$free_id){
				exit('err1');
			}
			$pdo = MyPdoCounty::init();
			if($c_type){ 	//封禁
				$stat = 3;
			}else{ 			//关闭
				$stat = 2;
			}
			$sql = 'update shop_free set free_stat = '.$stat.' where id=?';
			$res = $pdo->dml($sql,array($free_id));
			if($res){
				exit('ok');
			}else{
				exit('err');
			}
			break;



		//删除砍价商品
		case 'FreeGoodsDel':
			$free_id = intval(I('free_id','post'));
			//$store_id = intval(I('store_id','post'));
			$banner = $_POST['banner'];
			if(!$free_id || !$banner){
				exit('err1');
			}
			$pdo = MyPdoCounty::init();
			if($banner){
				$sql3 = 'update shop_img set fid = if(fid-1>0,fid-1,0),expire = ? where addr = ?';
				$res3 = $pdo->dml($sql3,array(time(),$banner));
			}
			$sql = 'delete from shop_free where id = ?';
			$res = $pdo->dml($sql,array($free_id));
			if($res){
				exit('ok');
			}else{
				exit('err1');	//删除砍价商品失败
			}
			break;


		//获取砍价商品列表 
		case 'FreeGoodsGetList':
			$store_id = intval(I('store_id','post'));
			$pdo = MyPdoCounty::Init();
			$sql = "select id,title,price,banner,free_stat from shop_free where store_id = ?";
			$res = $pdo->dql($sql,array($store_id));
			if($res){
				echo json_encode($res);
			}else{
				exit('err');
			}
			break;


		//添加砍价商品
		case 'FreeGoodsAdd':
			$store_id = intval(I('store_id','post'));
			$goods_id = intval(I('goods_id','post'));
			if(!$store_id || !$goods_id){
				exit('err1');
			}
			$pdo = MyPdoCounty::init();

			$sql1 = 'select title,price,banner from shop_goods where id = ?';
			$res1 = $pdo->dql($sql1,array($goods_id),1);
			if($res1){
				$sql2 = 'insert into shop_free(store_id,title,price,banner,ctime) values(?,?,?,?,?)';
				$res2 = $pdo->dml($sql2,array($store_id,$res1['title'],$res1['price'],$res1['banner'],time()));
				if($res2){
					//修改商品对应的free_id
					//$sql3 = 'update shop_goods set free_id = ? where id = ?';
					//$res3 = $pdo->dml($sql3,array($res2,$goods_id));
					//$sql4 = 'update shop_img set fid = ? where addr = ?';
					//$res4 = $pdo->dml($sql4,array($res2,$res1['banner']));
					//修改图片表中free项值 
					$sql4 = 'update shop_img set fid = fid+1 where addr = ?';
					$res4 = $pdo->dml($sql4,array($res1['banner']));
					exit('ok');
				}
			}
			break;




		//获取指定商户下未关联且常态的商品列表
		case 'GetNormalGoodsList';
			$store_id = intval(I('store_id','post'));
			$pdo = MyPdoCounty::init();
		//	$sql = "select id,title,price,number from shop_goods where store_id=? and goods_stat=2 and free_id=0";
			$sql = "select id,title,price,number from shop_goods where store_id=? and goods_stat=2 and store_stat = 1";
			$res = $pdo->dql($sql,array($store_id));
			if($res){
				echo json_encode($res);
			}else{
				exit('err');
			}
			break;




		//商品删除指定单张图片
		case 'GoodsImageDel':
			$img_id = I('img_id','post');
			$goods_id = I('goods_id','post');
			if(!$img_id || !$goods_id){
				exit('err1');
			}
			$pdo = MyPdoCounty::init();
			$sql1 = 'update shop_img set gid = 0,expire=? where id = ? and gid = ?';
			$res1 = $pdo->dml($sql1,array(time(),$img_id,$goods_id));
			if($res1){
				if(!@$_SESSION['img_update_stat'] || $_SESSION['img_update_stat']+5 < time()){
					$sql2 = 'update shop_goods set goods_stat = 1,utag1=utag1+1,utag3=utag3+1 where id = ?';
					$res2 = $pdo->dml($sql2,array($goods_id));
					$_SESSION['img_update_stat'] = time();
				}

				if(@$_SESSION['GoodsImageAdd_Count_'.$goods_id]){
					unset($_SESSION['GoodsImageAdd_Count_'.$goods_id]);
				}
				exit('ok');
			}else{
				exit('err');
			}
			break;



		//商品添加图片
		case 'GoodsImageAdd':
			$goods_id = intval(I('goods_id','post'));
			$store_id = intval(I('store_id','post'));
			if(!$goods_id || !$store_id){
				exit('err5');
			}
			$pdo = MyPdoCounty::init();
			if(@$_SESSION['GoodsImageAdd_Count_'.$goods_id]){
				$count = $_SESSION['GoodsImageAdd_Count_'.$goods_id] + 1;
			}else{
				$sql1 = "select count(gid) as c from shop_img where gid = ?";
				$res1 = $pdo->dql($sql1,array($goods_id),1);
				if($res1){
					if($res1['c'] > 0){
						$count = $res1['c'];
					}else{
						$count = 1;
					}
				}else{
					exit('err4');	//查询总条数异常
				}
			}
			if($count <= 50){		//判断商品图片是否超过20张
				$file = $_FILES['img'];
				$full_path = '/shop_img/'.$_SESSION['CountyInfo'][0].'/'.$store_id.'/'.$goods_id.'/'.date('YmdHis').'.jpg';
				$res_upyun = UploadImgToUpyun($config,$full_path,$file,1024*300);
				if(is_array($res_upyun)){
					$sql4 = "insert into shop_img(gid,addr) values(?,?)";
					$res4 = $pdo->dml($sql4,array($goods_id,$full_path));
					if($res4){
						if(!@$_SESSION['img_update_stat'] || $_SESSION['img_update_stat']+5 < time()){
							$sql3 = "update shop_goods set goods_stat=1,utag1=utag1+1,utag3=utag3+1 where id=?";
							$res3 = $pdo->dml($sql3,array($goods_id));
							$_SESSION['img_update_stat'] = time();
						}
						$_SESSION['GoodsImageAdd_Count_'.$goods_id] = $count;	//计数
						exit('ok');
					}else{
						UpyunDelFile($config,$full_path);
						echo 'err3';  //图片上传成功，但数据写入失败
					}
				}else{
					exit('err2'); //文件上传失败
				}
			}else{
				echo 'err1'; 	//图片数量超限!
			}
			break;


		//获取商品的图片列表
		case 'GetImgList':
			$gid = intval(I('gid','post'));
			$pdo = MyPdoCounty::init();
			$sql = "select id,addr from shop_img where gid = ?";
			$res = $pdo->dql($sql,array($gid));
			if($res){
				echo json_encode($res);
			}
			break;





		//获取商品列表
		case 'GetGoodsList':
			$store_id = intval(I('store_id','post'));
			$pdo = MyPdoCounty::init();
			$fields = 'id,title,price,number,banner,goods_stat,store_stat';
			$sql = "select {$fields} from shop_goods where store_id = ? and goods_stat != 6 order by id desc";
			$res = $pdo->dql($sql,array($store_id));
			if($res){
				echo json_encode($res);
			}
			//id,store_id,openid,tid1,tid2,tid3,title,price,info,number,banner,hit,ctime,goods_stat,store_stat,free_id 
			break;



		//获取商品信息
		case 'GetGoodsInfo':
			$id = intval(I('id','post'));
			$pdo = MyPdoCounty::init();
			$fields = 'id,tid1,tid2,tid3,type_set,title,price,info,number,banner';
			$sql = "select {$fields} from shop_goods where id = ?";
			$res = $pdo->dql($sql,array($id),1);
			if($res){
				echo json_encode($res);
			}
			break;



		//商品的上架下架
		case 'ChangeGoodsStat':
			$id = intval(I('id','post'));
			$stat = intval(I('stat','post'));
			$pdo = MyPdoCounty::init();
			$sql = 'update shop_goods set goods_stat = ? where id = ?';
			$res = $pdo->dml($sql,array($stat,$id));
			if($res){
				EditStoreUtag3($pdo,$id);	//修改商户下的商品列表版本utag3
				//还要对商品utag1和upyun做更新
				
				exit('ok');
			}else{
				exit('err');
			}
			//注：上下架时，立即更新商户的utag3
			break;



		//商品的删除
		case 'StoreGoodsDel':
			$id = intval(I('id','post'));
			$pdo = MyPdoCounty::init();

			$sql3 = 'select count(gid) as count from shop_img where gid = ?';
			$res3 = $pdo->dql($sql3,array($id),1);
			if($res3 && $res3['count'] > 0){
				//先解除商品关联的图片
				$sql = 'update shop_img set gid = 0,expire=? where gid = ?';
				$res = $pdo->dml($sql,array(time(),$id));
				if(!$res){
					exit('err');
				}
			}
			//将商品置为失效状态
			$sql2 = "update shop_goods set title='',info='',banner='',type_set='',ctime='',utag1=0,utag3=0,goods_stat = 6 where id = ?";
			$res2 = $pdo->dml($sql2,array($id));
			if($res2){

				EditStoreUtag3($pdo,$id);	//修改商户下的商品列表版本utag3

				//缓存数据到upyun
				//$upyun_path_update = '/detail_goods/dg_'.$_SESSION['CountyInfo'][0].'_'.$id.'.html';
				//UpyunAddFile($config,$upyun_path_update,'[]');

				$cache_name = '/detail_goods/dg_'.$_SESSION['CountyInfo'][0].'_'.$id;
				$cache_path = $cache_name.'.html';
				$cache_tag_path = $cache_name.'_utag1.html';
				UpyunAddFile($config,$cache_path,'[]');
				UpyunAddFile($config,$cache_tag_path,'none');
				exit('ok');
			}else{
				exit('err2');
			}

			//注：删除时，立即更新商户的utag3
			break;



		//商品的添加
		case 'StoreGoodsAdd':
			$code = I('code','post');
			Field_Check('/^.{1,}$/',$code,'err1');	//after-验证码不得为空!
			if(!CheckCode($code)){
				//exit('err2');	//验证码不正确！
			}

			$t1 = I('t1','post');
			$t2 = I('t2','post');
			$t3 = I('t3','post');
			$typeSet = I('typeSet','post');

			$store_id = I('store_id','post');
			$store_name = I('store_name','post');

			$openid = I('openid','post');

			if(!$t1 || !$t2 || !$t3 || !$store_id || !$store_name || !$openid){
				exit('err3');	//缺少参数！
			}

			
			//验证商户信息一致性
			if(!CheckStoreVCode($store_id,$store_name)){
				exit('ErrStoreVcode');
			}

			$title = I('title','post');
			Field_Check('/^.{1,50}$/',$title,'err4'); 	//after-商品名1-50个字符!

			$number = I('number','post')?:'';
			Field_Check('/^[0-9]{1,4}$|^$/',$number,'err5'); 	//after-编号为0-4四位数字!

			$price = I('price','post');
			Field_Check('/^[1-9][0-9]{0,8}(\.[0-9]{2})?$/',$price,'err6'); //after-价格不合法!

			$info = I('info','post');
			Field_Check('/^.{1,}$/',$info,'err7'); 	//after-商品详情不得为空!

			$pdo = MyPdoCounty::init();

			//检测商户下商品是否有失效商品，用来添加操作
			$sql3 = "select id from shop_goods where goods_stat = 6 limit 1";
			$res3 = $pdo->dql($sql3,array(),1);
			if($res3){	//有失效的商品记录，用来添加新商品
				//exit('bbbbbbb');
				$old_id = $res3['id'];
				$sql = "update shop_goods set tid1=?,tid2=?,tid3=?,type_set=?,title=?,price=?,info=?,number=?,ctime=?,goods_stat=1,banner='',utag1=1,utag2=0,utag3=1 where id = ?";
				$arr = array(
					$t1,
					$t2,
					$t3,
					$typeSet,
					$title,
					$price,
					$info,
					$number,
					time(),
					$old_id
				);
				$res = $pdo->dml($sql,$arr);
				if($res){
					exit('ok');
				}else{
					exit('err');
				}
			}


			$sql2 = "select count(id) count from shop_goods where store_id = ?";
			$res2 = $pdo->dql($sql2,array($store_id),1);
			if($res2 && $res2['count'] > 40){
				exit('err8');	//超过商品条数限制
			}
			$sql = "insert into shop_goods values(null,?,?,?,?,?,?,?,?,?,?,'',0,?,1,1,1,0,1)";
			$arr = array(
				$store_id,
				$openid,
				$t1,
				$t2,
				$t3,
				$typeSet,
				$title,
				$price,
				$info,
				$number,
				time(),
			);

			$new_id = $pdo->dml($sql,$arr);
			if($new_id && $new_id > 0){
				echo 'ok';
				//向微信发送模板通知信息
				//Wx_Template_Notice('待审核通知!',array('商户有新增商品请审核！',date('Y-m-d H:i:s')));
			}else{
				echo 'err9';	//添加失败!
			}

			//注：后台管理通过时修改商户的utag3和商品的utag1
			break;


		//商品的编辑
		case 'StoreGoodsEdit':
			
			$id = I('id','post');
			$code = I('code','post');
			Field_Check('/^.{1,}$/',$code,'err1');	//after-验证码不得为空!
			if(!CheckCode($code)){
				//exit('err2');	//验证码不正确！
			}

			$t1 = I('t1','post');
			$t2 = I('t2','post');
			$t3 = I('t3','post');
			$type_set = I('type_set','post');
			if(!$t1 || !$t2 || !$t3 || !$type_set){
				exit('err3');	//缺少参数！
			}

			$banner = @$_POST['banner'];

			$title = I('title','post');
			Field_Check('/^.{1,50}$/',$title,'err4'); 	//after-商品名1-50个字符!

			$number = I('number','post')?:'';
			Field_Check('/^[0-9]{1,4}$|^$/',$number,'err5'); 	//after-编号为0-4四位数字!

			$price = I('price','post');
			Field_Check('/^[1-9][0-9]{0,8}(\.[0-9]{2})?$/',$price,'err6'); //after-价格不合法!

			$info = I('info','post');
			Field_Check('/^.{1,}$/',$info,'err7'); 	//after-商品详情不得为空!

			$pdo = MyPdoCounty::init();
			$sql = 'update shop_goods set tid1=?,tid2=?,tid3=?,type_set=?,title=?,number=?,price=?,info=?,banner=?,goods_stat=1,utag1=utag1+1 where id=?';
			$res = $pdo->dml($sql,array($t1,$t2,$t3,$type_set,$title,$number,$price,$info,$banner,$id));
			if($res){
				exit('ok');
			}else{
				exit('err4');
			}

			//注：在后台管理通过时才对该商户的utag3做更新
			break;








	/*	//全局服获取商品类别列表
		case 'GetGoodTypes':
			$pdo = MyPdoShare::init(1);
			$sql = "select * from share_goods_type";
			$res = $pdo->dql($sql,array());
			echo json_encode($res);
			break;*/


		//将商户的县库信息缓存到session
		case 'SetStoreAreaInfoToSession':
			$city_code = I('city_code','post');
			$db_host = I('db_host','post');
			$db_name = I('db_name','post');
			if(!$city_code || !$db_host || !$db_name){
				exit('err');
			}
			$arr[] = $city_code;
			$arr[] = $db_host;
			$arr[] = $db_name;
			$_SESSION['CountyInfo'] = $arr;
			if(count($_SESSION['CountyInfo']) >=3){
				exit('ok');
			}else{
				exit('err2');
			}
			break;



		//获取商户信息
		case 'GetStoreInfo':
			$openid = I('openid','post');
			if(!$openid){
				exit('err1');	//缺少openid
			}

			//向全局库获取商户的库址记录
			$pdo = MyPdoShare::init(1);
			//### 注 , 将来同时需要在upyun上做静态缓存 
			$sql = "select city_code,db_host,db_name from share_openid_server where openid = ?";
			$res = $pdo->dql($sql,array($openid),1);
			if($res){	//获取到县库址
				if(count($res) < 3){
					exit('err2');		//县服参数异常
				}
				foreach($res as $v){
					$city_info[] = $v;
				}
				$_SESSION['CountyInfo'] = $city_info;

				//向县库获取商户数据
				$pdo2 = MyPdoCounty::init(1);
				$sql2 = "select * from shop_store where openid = ?";
				$res2 = $pdo2->dql($sql2,array($openid),1);
				if($res2){
					echo $store_info = json_encode($res2);

					//缓存商户验证码
					$v_code = SetFieldsVCode($res2['id'],$res2['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
					$_SESSION['StoreVCode'] = $v_code;

					//向upyun做商户状态标记缓存
					$store_info2 = 'Gets('.$store_info.')';
					$path = '/Openid/'.$openid.'.html';
					UpyunAddFile($config,$path,$store_info2);
				}else{
					exit('err3'); 	//全局库有记录，但全局缓存县服无商户信息，数据丢失
				}
			}else{	//全局服没有记录，跳转到注册页
				exit('err4');
			}
			break;



		//绑定注册或编辑商户信息
		case 'StoreInfoAddOrEdit':
			$code = I('code','post'); 
			if(!CheckCode($code)){
				//exit('err1');		//验证码不正确
			}
			$openid = I('openid','post');
			if(!$openid){
				exit('err2');	//缺少openid
			}


			//如果是注册，需要城市信息
			if(@$_POST['county']){
				$county = I('county','post'); 
				$countys = explode(',',$county);
				if(count($countys) < 3){
					exit('err3');	//城区信息异常
				}
				$_SESSION['CountyInfo'] = $countys;
				$city_code = $countys[0];
				$db_host   = $countys[1];
				$db_name   = $countys[2];

				$CityFullName = I('CityFullName','post'); 
			}else{  	//如果是修改
				$county = I('city_val','post'); 
				$countys = explode(',',$county);
				if(count($countys) < 3){
					exit('err3');	//区域信息异常
				}
				$_SESSION['CountyInfo'] = $countys;
			}

			//echo $county;exit;

			$name = I('name','post'); 
			Field_Check('/^.{3,20}$|^[\x{4e00}-\x{9fa5}]{3,20}$/u',$name,'商户名长度为3-20个字符!','Void_Token');

			$phone = I('phone','post'); 
			Field_Check('/^[1-9][0-9]{6,11}$/',$phone,'电话格式不正确!','Void_Token');

			$qq = I('qq','post')?:'';
			Field_Check('/^[1-9][0-9]{4,11}$|^$/',$qq,'QQ号不正确!','Void_Token');

			$wx = I('wx','post')?:''; 
			Field_Check('/^[a-zA-z0-9_-]{3,30}$|^$/',$wx,'微信号格式不正确!','Void_Token');

			$addr = I('addr','post')?:''; 
			Field_Check('/^.{1,50}$|^[\x{4e00}-\x{9fa5}]{1,50}$|^$/u',$addr,'地址长度不超过50个字符!','Void_Token');

			$info = I('info','post'); 
			Field_Check('/^.{1,300}$|^[\x{4e00}-\x{9fa5}]{1,300}$|^$/u',$info,'商户描述不得为空且不得超过300字符!','Void_Token');


			$banner = '';

			if($_FILES){
				$imgs = $_FILES['banner'];
				$full_path = '/StoreBanner/'.$countys[0].'/'.$openid.'_banner.jpg'; 	
				//上传图片到upyun ,检测 ，缩放组合函数
				$res_upyun = UploadImgToUpyun($config,$full_path,$imgs,1024*200);
				if(is_array($res_upyun)){
					$banner = $full_path.'?t='.rand(1,9999);
				}else{
					exit('err5'); //文件上传失败
				}
			}
			$pdo = MyPdoCounty::init();
			$sql = "select banner,stat from shop_store where openid = ?";
			$res = $pdo->dql($sql,array($openid),1);
			if($res){ 	//修改商户信息
				if($res['stat'] == 4){
					exit('err6');	//商户已封禁
				}
				if($banner){	//判断编辑时是否修改了封面
					$sql2 = "update shop_store set stat = 1,name=?,addr=?,info=?,phone=?,wx=?,qq=?,banner=?,utag1=utag1+1 where openid=?";
					$sql_arr = array($name,$addr,$info,$phone,$wx,$qq,$banner,$openid);
				}else{
					$sql2 = "update shop_store set stat = 1,name=?,addr=?,info=?,phone=?,wx=?,qq=?,utag1=utag1+1 where openid=?";
					$sql_arr = array($name,$addr,$info,$phone,$wx,$qq,$openid);
				}
				$res2 = $pdo->dml($sql2,$sql_arr);
				if($res2){
					$sql4 = 'select * from shop_store where openid = ?';
					$res4 = $pdo->dql($sql4,array($openid),1);
					if($res4){
						echo $res6 = json_encode($res4);


						//缓存商户信息验证码
						$v_code = SetFieldsVCode($res4['id'],$res4['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
						$_SESSION['StoreVCode'] = $v_code;


						$res5 = 'Gets('.$res6.')';
						$path = '/Openid/'.$openid.'.html';
						//缓存商户信息到全局服
						UpyunAddFile($config,$path,$res5); 
					}else{
						echo 'err9'; //修改成功后获取商户信息失败
					}
				}else{
					exit('err7');	//数据没有任何改动
				}
			}else{ 	//注册商户信息
				$sql3 = "insert into shop_store values(null,?,?,?,?,?,?,?,?,0,1,?,?,?,?,?,0,0,0,0,0)";
				$id_new = $pdo->dml($sql3,array($openid,$name,$addr,$info,$phone,$wx,$qq,$banner,time(),$city_code,$db_host,$db_name,$CityFullName));
				if($id_new){
					//向全局服缓存openid及县服信息
					$pdo_s = MyPdoShare::init(1);
					$sql_s = 'insert ignore into share_openid_server values(null,?,?,?,?)';
					$res_s = $pdo_s->dml($sql_s,array($openid,$city_code,$db_host,$db_name));
					if($res_s){
						//记录到全局服后，获取注册好的信息，
						$sql4 = 'select * from shop_store where id=?';
						$res4 = $pdo->dql($sql4,array($id_new),1);
						if($res4){
							echo $res6 = json_encode($res4);

							//缓存商户信息验证码
							$v_code = SetFieldsVCode($res4['id'],$res4['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
							$_SESSION['StoreVCode'] = $v_code;

							$res5 = 'Gets('.$res6.')';
							$path = '/Openid/'.$openid.'.html';
							//缓存商户信息到全局服
							UpyunAddFile($config,$path,$res5); 
						}else{
							$err_info = "县服异常通知\r\nopenid：".$openid."\r\n商户id：".$id_new."\r\ninfo："."\r\n地区码：".$city_code."\r\n库址：".IpDecrypt($db_host)."\r\n库名：".$db_name;
							Wx_Template_Notice('注册后获取商户信息失败！',array($err_info,date('Y-m-d H:i:s')));
							exit('err8');	//获取商户信息失败
						}
					}else{
						$err_info = "全局服异常通知\r\nopenid：".$openid."\r\n商户id：".$id_new."\r\ninfo："."\r\n地区码：".$city_code."\r\n库址：".IpDecrypt($db_host)."\r\n库名：".$db_name;
						Wx_Template_Notice('商户openid全局缓存失败！',array($err_info,date('Y-m-d H:i:s')));
						exit('err9');	//注册成功，向全局服记录商户对应县服信息失败
					}
				}else{
					exit('err10');	//注册失败
				}
			}

			break;







		//向全局服获取城市信息
		case 'GetAreaInfo':
				$pdo = MyPdoShare::init();
				$sql = 'select * from share_city';
				$res = $pdo->dql($sql,array());
				Merge_Or_Make_Cache($res,'Cache/City/city');
			break;





		//商户登陆时缓存县库信息
		case 'SetDbInfo':
			$db_info = I('db_info','post');
			$store_id = I('store_id','post');
			$store_name = I('store_name','post');
			if(!$db_info || !$store_id || !$store_name){
				exit('err2');
			}
			$_SESSION['CountyInfo'] = explode(',',$db_info);
			if($_SESSION['CountyInfo'] && $_SESSION['CountyInfo'][2]){
				$v_code = SetFieldsVCode($store_id,$store_name,$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
				$_SESSION['StoreVCode'] = $v_code;
				if(@$_SESSION['StoreVCode']){
					 exit('ok');
				}else{
					 exit('err3');
				}
			}else{
				 exit('err');
			}
			break;



		case 'QueryStoreStat':
			$id = intval(I('id','post'));
			$pdo = MyPdoCounty::init();
			$sql = 'select stat from shop_store where id=?';
			$res = $pdo->dql($sql,array($id),1);
			if($res){
				echo $res['stat'];
			}
			break;



		case 'GetCityFullInfo':
			$pdo = MyPdoShare::init();
			if(I('open_type','post')){
				$sql = "select id,code,name,parent_id,level,db_host,db_name from share_city where level=2 and db_host != '' and db_name != ''";
			}else{
				$sql = "select id,code,name,parent_id,level,db_host,db_name from share_city";
			}
			$res = $pdo->dql($sql,array());
			if($res){
				$res2 = json_encode($res);
				$res3 = 'Gets('.$res2.')';
				$path = '../Cache/CityInfo/update_stat';
				if(I('open_type','post') && file_exists($path)){
					UpyunAddFile($config,'CityInfo/CityOpen.html',$res3);
					unlink($path);
				}
				exit($res3);
			}else{
				$res3 = 'Gets([])';
				$path = '../Cache/CityInfo/update_stat';
				if(I('open_type','post') && file_exists($path)){
					UpyunAddFile($config,'CityInfo/CityOpen.html',$res3);
					unlink($path);
				}
				exit('err');
			}
			break;







		default :
			echo 'default';
			dump($_SESSION);
			dump($_COOKIE);
			break;

	}






//检测验证码
function CheckCode($code=''){
	require_once "../Class/Plugin/VCode.class.php";
	if(Code\VCode::Check_Code($code)){
		return true;
	}else{
		return false;
	}
}


//验证商户信息一致性
function CheckStoreVCode($sid,$sname){
	$v_code_sess = $_SESSION['StoreVCode'];
	$v_code = SetFieldsVCode($sid,$sname,$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
	if($v_code_sess != $v_code){
		return false;
	}
	return true;
}


//更新商户下的商品列表版本utag3
function EditStoreUtag3($pdo,$goods_id){
	$sql3 = 'update shop_store set utag1=utag1+1,utag3=utag3+1 where id = (select store_id from shop_goods where id = ?)';
	$res3 = $pdo->dml($sql3,array($goods_id));
}